package com.hb.trans.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import com.hb.trans.greendao.DaoMaster;
import com.hb.trans.greendao.DaoSession;



/**
 * 本地数据库控制类
 */
public class DBLocalController
{
    private static DaoMaster daoMasterEcmc;

    //默认DB
    private static DaoSession daoSessionEcmc;
    private static Context mContext;
    private static PackageInfo packInfo;

    private static DaoMaster obtainMaster(Context context, String dbName)
    {
        return new DaoMaster(new DaoMaster.DevOpenHelper(context, dbName, null).getWritableDatabase());
//        return new DaoMaster(new MySQLiteOpenHelper(context, dbName, null).getWritableDatabase());
    }

    public DBLocalController(Context context){
       mContext = context;
    }


    private DaoMaster getDaoMaster(Context context, String dbName)
    {
        if (dbName == null) {
            return null;
        }
        if (daoMasterEcmc == null)
        {
            daoMasterEcmc = obtainMaster(context, dbName);
        }
        return daoMasterEcmc;
    }

    /**
     * 取得DaoSession
     *
     * @return
     */
    public DaoSession getDaoSession(String dbName)
    {

        if (daoSessionEcmc == null)
        {
            daoSessionEcmc = getDaoMaster(mContext, dbName).newSession();
        }
        return daoSessionEcmc;
    }

}
